import React from "react";
import NavHeader from "../../components/NavHeader";
import HouseItem from "../../components/HouseItem";
import { API, BASE_URL } from "../../utils";
import styles from './index.module.css'

class Favorite extends React.Component {
  state = {
    favoriteList: [], // 存储收藏列表状态
  };

  componentDidMount() {
    this.getFavoriteHouseList();
  }
  // 获取收藏列表
  getFavoriteHouseList = async () => {
    // console.log(1);
    const res = await API.get("/user/favorites");
    // console.log(res);
    this.setState({
      favoriteList: res.data.body,
    });
  };
  // 点击跳转到对应租房详情页
  handleClick = (id) => {
    this.props.history.push(`/housedetail/${id}`)
  }

  render() {
    const { favoriteList } = this.state;
    return (
      <div className={styles.favorites}>
        {/* 头部导航栏 */}
        <div className={styles.navHeader}>
        <NavHeader>收藏页面</NavHeader>
        </div>
        {/* <HouseItem></HouseItem> */}
        {/* 展示收藏列表 */}
        <div className={styles.houseItems}>
          {favoriteList.map((item) => {
            return (
              <HouseItem
                {...item}
                src={BASE_URL + item.houseImg}
                onClick={() => this.handleClick(item.houseCode)}
                key={item.houseCode}
              ></HouseItem>
            );
          })}
        </div>
      </div>
    );
  }
}

export default Favorite;
